package com.cheng.controller;

import com.cheng.common.exception.CommonException;
import com.cheng.common.results.DataRslt;
import com.cheng.common.utils.CodeUtil;
import com.cheng.data.vo.DepartmentVO;
import com.cheng.data.vo.ReqPageInfoVO;
import com.cheng.data.vo.RespPageInfoVO;
import com.cheng.operation.DepartmentOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: ChengJW
 * @date: 2021/11/10 8:12
 * @version: 1.0
 * @description: 部门管理页面处理器
 */
@Controller
public class DepartmentAdminController extends BaseController{

    private final static Logger log = LoggerFactory.getLogger(DepartmentAdminController.class);

    @Autowired
    private DepartmentOperation departmentOperation;

    @RequestMapping("/systemDefend/departmentAdmin")
    public String departmentAdmin(){
        return "departmentAdmin";
    }

    /**
    * @author: ChengJW
    * @params: []
    * @return: java.lang.String
    * @date: 2021/11/10
    * @description: 新增部门信息
     */
    @RequestMapping("/systemDefend/departmentAdmin/addDepartmentInfo")
    public String departmentAdminAdd(){
        return "addDepartmentInfo";
    }

    /**
    * @author: ChengJW
    * @params: []
    * @return: java.lang.String
    * @date: 2021/11/12
    * @description: 修改部门信息
    */
    @RequestMapping("/systemDefend/departmentAdmin/updateDepartmentInfo")
    public String departmentAdminUpdate(){
        return "updateDepartmentInfo";
    }

    /**
    * @author: ChengJW
    * @params: [vo]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/11
    * @description: 查询部门信息，并返回 layui 框架统一封装VO
    */
    @RequestMapping("/systemDefend/departmentAdmin/selectDepartmentInfo")
    @ResponseBody
    public DataRslt selectDepartmentInfo(ReqPageInfoVO reqPageInfoVO) throws CommonException {

        reqPageInfoVO.setUserCode(getUrCode());
        RespPageInfoVO respPageInfoVO= departmentOperation.selectDepartmentInfoList(reqPageInfoVO);
        return tabSuccess(respPageInfoVO);
    }

    /**
    * @author: ChengJW
    * @params: []
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/11
    * @description: 新增部门信息时，初始化回显部门编号、所属单位信息
    */
    @RequestMapping("/systemDefend/departmentAdmin/initDepartmentInfo")
    @ResponseBody
    public DataRslt initDepartmentInfo() throws CommonException{

        DepartmentVO departmentVO = new DepartmentVO();
        departmentVO.setUserCode(getUrCode());
        DepartmentVO vo = departmentOperation.initAddDepartmentInfo(departmentVO);
        return success(vo);
    }

    /**
    * @author: ChengJW
    * @params: [departmentVO]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/11
    * @description: 新增部门信息
    */
    @RequestMapping("/systemDefend/departmentAdmin/insertDepartment")
    @ResponseBody
    public DataRslt insertDepartmentInfo(DepartmentVO departmentVO) throws CommonException{

        int count = departmentOperation.insertDepartmentInfo(departmentVO);
        if (count > 0){
            return success();
        }else if (count == -1){
            return failure(CodeUtil.CODE_MAP.get("CJW202111010032"));
        }
        return failure(CodeUtil.CODE_MAP.get("CJW202111010033"));
    }

    /**
    * @author: ChengJW
    * @params: [departmentVO]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/12
    * @description: 修改部门信息
    */
    @RequestMapping("/systemDefend/departmentAdmin/updateDepartment")
    @ResponseBody
    public DataRslt updateDepartmentInfo(DepartmentVO departmentVO) throws CommonException{

        int count = departmentOperation.updateDeapartmentInfo(departmentVO);
        if (count > 0){
            return success();
        }
        // 0 表示修改部门信息失败
        return failure(CodeUtil.CODE_MAP.get("CJW202111010034"));
    }

    /**
    * @author: ChengJW
    * @params: [departmentVO]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/12
    * @description: 删除部门信息
    */
    @RequestMapping("/systemDefend/departmentAdmin/deleteDepartment")
    @ResponseBody
    public DataRslt deleteDepartmentInfo(DepartmentVO departmentVO) throws CommonException{
        int count = departmentOperation.deleteDepartmentInfo(departmentVO);
        if (count > 0){
            return success();
        }
        // 0 表示删除部门信息失败
        return failure(CodeUtil.CODE_MAP.get("CJW202111010034"));
    }
}
